package com.warren.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.warren.annotation.ControllerEndpoint;
import com.warren.handler.BusinessException;
import com.warren.response.Result;
import com.warren.response.ResultCode;
import com.warren.system.entity.Department;
import com.warren.system.service.DepartmentService;
import com.warren.system.vo.DeptVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author warren
 * @since 2021-03-22
 */
@Api(value = "部门管理",tags = "部门管理接口")
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @ApiOperation(value = "查询部门及人数",notes = "从部门表中分组查询部门人数")
    @RequestMapping(value = "/findDeptAndCount",method = RequestMethod.GET)
    public Result findDeptAndCount(){
        List<Department> departments = departmentService.findDeptAndCount();
        if (departments.size() == 0){
            throw  new BusinessException(ResultCode.DEPARTMENT_NOT_EXIST.getCode(),
                    ResultCode.DEPARTMENT_NOT_EXIST.getMessage());
        }
        return Result.ok().data("department",departments);
    }

    @ApiOperation(value = "查询部门")
    @PostMapping("/getDeptList")
    public Result getDeptList(@RequestBody DeptVO deptVO){
        QueryWrapper<Department> queryWrapper = getWrapper(deptVO);
        List<Department> departments = departmentService.getDeptAndCount(queryWrapper);
        return Result.ok().data("departments",departments);
    }

    @ControllerEndpoint(exceptionMessage = "添加部门失败",operation = "添加部门失败")
    @ApiOperation(value = "添加部门")
    @PostMapping("/addDept")
    public Result addDept(@RequestBody Department department){
        return departmentService.addDept(department);
    }

    @ControllerEndpoint(exceptionMessage = "修改部门失败",operation = "修改部门")
    @ApiOperation(value = "修改部门")
    @PutMapping("/updateDept")
    public Result updateDept(@RequestBody Department department){
        return departmentService.updateDept(department);
    }

    @ControllerEndpoint(exceptionMessage = "删除部门失败",operation = "删除部门")
    @ApiOperation(value = "删除部门")
    @DeleteMapping("/deleteDept")
    public Result deleteDept(Long id){
        return departmentService.deleteDept(id);
    }

    @ControllerEndpoint(exceptionMessage = "导出部门excel失败",operation = "导出部门excel")
    @ApiOperation(value = "导出部门excel",notes = "导出所有部门的excel表格")
    @PostMapping("/excel")
    public void export(HttpServletResponse response)throws IOException {
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        List<Department> departments = departmentService.getDeptAndCount(queryWrapper);
        departmentService.exportDeptExcel(response,departments);
    }

    /**
     * 筛选条件
     * @param deptVO
     * @return
     */
    private QueryWrapper<Department> getWrapper(DeptVO deptVO){
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        if (deptVO != null){
            //查询每个条件
            if (!ObjectUtils.isEmpty(deptVO.getDeptName())){
                queryWrapper.like("name",deptVO.getDeptName());
            }
        }

        return  queryWrapper;
    }
}

